package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hanhai.zrb.api.biz.dao.mybatis.SysDictionaryMapper;
import com.hanhai.zrb.api.biz.service.SysDictionaryService;
import com.hanhai.zrb.model.common.SysDictionary;

@Service("sysDictionaryService")
public class SysDictionaryServiceImpl implements SysDictionaryService{

	Map<String,SysDictionary> sysDictionaryMap = new HashMap<String,SysDictionary>();
	
	Map<String,List<SysDictionary>> sysDictionaryListMap = new HashMap<String,List<SysDictionary>>();

	@Resource
	private SysDictionaryMapper sysDictionaryMapper;
	
	@PostConstruct
	void init() {
		List<SysDictionary> sysDictionaryList = this.listAll();
		Map<String,SysDictionary> tempSysDictionaryMap = new HashMap<String,SysDictionary>();
		Map<String,List<SysDictionary>> tempSysDictionaryListMap = new HashMap<String,List<SysDictionary>>();
		for (SysDictionary sysDictionary : sysDictionaryList) {
			tempSysDictionaryMap.put(sysDictionary.getItemCode(), sysDictionary);
			String topCode = sysDictionary.getTopItemCode();
			List<SysDictionary> topList = tempSysDictionaryListMap.get(topCode);
			if(topList == null){
				topList = new ArrayList<SysDictionary>();
				tempSysDictionaryListMap.put(topCode, topList);
			}
			topList.add(sysDictionary);
		}
		this.sysDictionaryMap = tempSysDictionaryMap;
		this.sysDictionaryListMap = tempSysDictionaryListMap;
	}
	
	
	@Scheduled(cron = "0 0/1 * * * ? ")
	@Override
	public void refresh() {
		init();
	}
	
	@Override
	public SysDictionary getByCodeFromLocalCache(String code){
		return sysDictionaryMap.get(code);
	}
	
	@Override
	public List<SysDictionary> listByTopCodeFromLocalCache(String topCode){
		return sysDictionaryListMap.get(topCode);
	}
	
	@Override
	public SysDictionary selectByPrimaryKey(Long id) {
		return sysDictionaryMapper.selectByPrimaryKey(id);
	}

	@Override
	public int deleteByPrimaryKey(Long id) {
		return sysDictionaryMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(SysDictionary record) {
		return sysDictionaryMapper.insert(record);
	}

	@Override
	public int insertSelective(SysDictionary record) {
		return sysDictionaryMapper.insertSelective(record);
	}

	@Override
	public int updateByPrimaryKeySelective(SysDictionary record) {
		return sysDictionaryMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(SysDictionary record) {
		return sysDictionaryMapper.updateByPrimaryKey(record);
	}

	@Override
	public PageInfo findPageByKeyWord(SysDictionary sysDictionary,int pageNum,int pageSize) {
		PageHelper.startPage(pageNum, pageSize, true);
		List<SysDictionary> list =sysDictionaryMapper.findByKeyWord(sysDictionary);
		for(SysDictionary dic:list){
			SysDictionary tempDic = sysDictionaryMapper.selectByCode(dic.getTopItemCode());
			dic.setParentItem(null==tempDic?"":tempDic.getItemName());
		}
		return new PageInfo(list);
	}

	@Override
	public SysDictionary selectByCode(String itemCode) {
		return sysDictionaryMapper.selectByCode(itemCode);
	}

	@Override
	public List<SysDictionary> getFirstLevelItem() {
		return sysDictionaryMapper.getFirstLevelItem();
	}

	@Override
	public List<SysDictionary> getItemByParentCode(String parentCode) {
		return sysDictionaryMapper.getItemByParentCode(parentCode);
	}

	@Override
	public Map<String, SysDictionary> getItemsMap(SysDictionary sysDictionary) {
		Map<String, SysDictionary> map = new HashMap<String, SysDictionary>();
		List<SysDictionary> list =sysDictionaryMapper.findByKeyWord(sysDictionary);
		if(CollectionUtils.isNotEmpty(list)){
			for(SysDictionary sys:list){
				map.put(sys.getItemCode(), sys);
			}
		}
		return map;
	}
	public List<SysDictionary> listAll() {
		return sysDictionaryMapper.selectAll();
	}
}
